package com.example.benefitcommunity.service.impl;

import com.example.benefitcommunity.entity.BcUser;
import com.example.benefitcommunity.dao.BcUserDao;
import com.example.benefitcommunity.entity.Result;
import com.example.benefitcommunity.enums.StatusCode;
import com.example.benefitcommunity.service.BcUserService;
import com.example.benefitcommunity.util.JWTUtil;
import com.example.benefitcommunity.util.UserNameUtil;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户登录表(BcUser)表服务实现类
 *
 * @author makejava
 * @since 2025-08-04 09:39:12
 */
@Transactional(rollbackFor = Exception.class)
@Service("bcUserService")
public class BcUserServiceImpl implements BcUserService {
    @Resource
    private BcUserDao bcUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BcUser queryById(Integer id) {
        return this.bcUserDao.queryById(id);
    }

    /**
     * 查询
     *
     * @param username 用户
     * @param password 密码
     * @return 查询结果
     */
    @Override
    public Result<Object> queryByUserNameAndPassword(String username, String password) {
        Map<String, Object> map = new HashMap<>(30);
        BcUser bcUser = this.bcUserDao.queryByName(username);
        if (bcUser != null && bcUser.getPassword().equals(password)) {
            map.put("BcUser", bcUser);
            map.put("token", JWTUtil.token(bcUser));
            return new Result<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMsg(), 1L, map);
        }
        return new Result<>(StatusCode.ERROR.getCode(), "用户名或密码错误,若没有账户请注册", 0L, null);
    }

    /**
     * 注册
     *
     * @param username 账户
     * @param phone    实际
     * @param password 密码
     * @return 注册结果
     */
    @Override
    public Result<Object> register(String username, String phone, String password) {
        if (this.bcUserDao.queryByName(username) == null) {
            BcUser bcUser = new BcUser();
            bcUser.setUsername(username);
            bcUser.setPassword(password);
            bcUser.setPhone(phone);
            bcUser.setNickname(UserNameUtil.generateUniqueUsername());
            bcUser.setBirthday(new Date());
            bcUser.setDescription("描述一下吧");
            if (this.bcUserDao.insert(bcUser) > 0) {
                return new Result<>(StatusCode.SUCCESS.getCode(), "注册成功", 1L, null);
            }
            return new Result<>(StatusCode.ERROR.getCode(), "注册失败", 0L, null);
        }
        return new Result<>(StatusCode.ERROR.getCode(), "用户已存在", 0L, null);
    }

    /**
     * 忘记密码
     *
     * @param username    账户
     * @param password 密码
     * @return 忘记密码结果
     */
    @Override
    public Result<Object> forgotPassword(String username, String password) {
        BcUser bcUser=this.bcUserDao.queryByName(username);
        if (bcUser != null) {
            BcUser bcUserNew = new BcUser();
            bcUserNew.setId(bcUser.getId());
            bcUserNew.setPassword(password);
            if (this.bcUserDao.update(bcUserNew) > 0) {
                return new Result<>(StatusCode.SUCCESS.getCode(), "密码更新成功", 1L, null);
            }
            return new Result<>(StatusCode.ERROR.getCode(), "密码更新失败", 0L, null);
        }
        return new Result<>(StatusCode.ERROR.getCode(), "用户不存在,请注册", 0L, null);
    }

    /**
     * 修改个人信息
     *
     * @param bcUser
     * @return 返回
     */
    @Override
    public Result<Object> personInfo(BcUser bcUser) {
        Map<String, Object> map = new HashMap<>(30);
        BcUser bcUserNew = this.bcUserDao.queryById(bcUser.getId());
        if (bcUserNew != null) {
            if (this.bcUserDao.update(bcUser) > 0) {
                map.put("BcUser", bcUser);
                return new Result<>(StatusCode.SUCCESS.getCode(), "个人信息更新成功", 1L, map);
            }
            return new Result<>(StatusCode.ERROR.getCode(), "个人信息更新失败", 0L, null);
        }
        return new Result<>(StatusCode.ERROR.getCode(), "用户不存在", 0L, null);
    }

    /**
     * 分页查询
     *
     * @param bcUser      筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<BcUser> queryByPage(BcUser bcUser, PageRequest pageRequest) {
        long total = this.bcUserDao.count(bcUser);
        return new PageImpl<>(this.bcUserDao.queryAllByLimit(bcUser, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param bcUser 实例对象
     * @return 实例对象
     */
    @Override
    public BcUser insert(BcUser bcUser) {
        this.bcUserDao.insert(bcUser);
        return bcUser;
    }

    /**
     * 修改数据
     *
     * @param bcUser 实例对象
     * @return 实例对象
     */
    @Override
    public BcUser update(BcUser bcUser) {
        this.bcUserDao.update(bcUser);
        return this.queryById(bcUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.bcUserDao.deleteById(id) > 0;
    }
}
